#!/usr/bin/python 
def get_countof1(bit):
    test = str(bit)
    if len(test) == 1:
        if bit >= 1:
            return 1
        else:
            return 0
    if len(test) == 2:
        if test[0] == '1':
            return special(test)
        else:
            return 10 + int(test[0])*funit(1) + get_countof1(int(test[1]))
    else:
        if test[0] == '1':
            sure_count = 1 + int(test[1:]) 
        else:
            sure_count = 10 ** (len(test) - 1)
        return sure_count + int(test[0])*funit(len(test)-1) + get_countof1(int(test[1:]))
def special(bit):
    if bit == '10':
        return 2
    elif bit == '11':
        return 4
    else:
        return int(bit[1]) + 3
def funit(width):
    if width == 1:
        return 1
    else:
        return 10 ** (width - 1) + 10 * funit(width -1)

def get_traverse(number):
    sum = 0
    for i in xrange(number+1):
        temp = str(i)
        for j in xrange(len(temp)):
            if temp[j] == '1':
                sum += 1
    return sum
testvalue=98765432L
#for testvalue in xrange(1, 10000, 1): 
r2 = get_countof1(testvalue)
print "value : %ld \nresult: %ld" % (testvalue, r2)
#r1 = get_traverse(testvalue)
#print "value: %d tarverse result: %d " % (testvalue, r1)
    #if r1 != r2:
#print get_traverse(322)
#print get_traverse(654)
